package com.example.webfluxx;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@SpringBootApplication
public class WebfluxxApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebfluxxApplication.class, args);
    }

    @Bean
    public RouterFunction<ServerResponse> heellwordl() {
        return route(GET("/hello-world"), serverRequest -> ok().body(Mono.just("hello,world"), String.class));
    }

    /**
     * 只对web应用有效
     *
     * @param context
     * @return
     */
    @Bean
    public ApplicationRunner runner(WebServerApplicationContext context) {
        return args -> {
            System.out.println("1当前webserver实现类为" + context.getWebServer().getClass().getName());
        };
    }

    /**
     * 监听事件，什么应用都可以
     *
     * @param event
     * @return
     */
    @EventListener(WebServerInitializedEvent.class)
    public void onWebServerReady(WebServerInitializedEvent event) {
        System.out.println("2当前webserver实现类为" + event.getWebServer().getClass().getName());
    }
}
